<?php
/**
 * Created by PhpStorm.
 * User: Aliaksei Lazerka
 * Date: 9/28/14
 * Time: 12:33 PM
 */

namespace log;

if (!defined('NYMPH_ROOT')) {
  define('NYMPH_ROOT', dirname("../../../" . __FILE__));
  require(NYMPH_ROOT . '/local/loader/kernel/classes/URLClassLoader.php');
}

/**
 * The LoggerFactory class provides loggers
 */
class LoggerFactory
{

  /**
   * @var LoggerFactory
   */
  private static $loggerFactory;

  const BROWSER_LOGGER_TYPE = 'browser';
  const FILE_LOGGER_TYPE = 'file';

  function __construct($configFiles)
  {
    $this->xmlParser = new XMLParserImpl($configFiles);
    $this->loggers = $this->xmlParser->parseAll();
    self::$loggerFactory = $this;
  }

  /**
   * @var array Logger associated array with available loggers
   * key - logger name
   */
  private $loggers;

  /**
   * @var XMLParserImpl user for parsing config
   * files and creating loggers
   */
  private $xmlParser;

  /**
   * Returns logger from loggerFactory
   * @param string $loggerName
   * @return Logger logger
   */
  public function getLoggerByName($loggerName)
  {
    foreach ($this->loggers as $logger) {
      if ($logger->getLogConfig()->getName() == $loggerName) {
        return $logger;
      }
    }
  }

  /**
   * @return LoggerFactory
   */
  public function getFactory()
  {
    return self::$loggerFactory;
  }
}